import { Store } from 'iflux2';
import { List,fromJS} from 'immutable'
import ArticleActor from '../actor/article-actor.js';
import SearchActor from '../actor/search-actor.js'
import {ArticleTypes, SectionTypes, SearchTypes} from '../constants/actionTypes.js'
import webapi from './webapi.js';
import { message } from 'antd'
import PaginationActor from 'actor/pagination'
import SectionActor from '../actor/section-actor.js'
import { PaginationType } from 'actor/pagination/type'
import DomainActor, {DomainTypes} from 'actor/domain/index';
export default class AppStore extends Store {
    bindActor () {
        return [
            new ArticleActor,
            new PaginationActor,
            new SectionActor,
            new DomainActor,
            new SearchActor,
        ]
    }

    constructor(props){
        super(props);
    }
    fetchDomain = () => {
        webapi.fetchDomain().then(result => {
            this.dispatch(DomainTypes.RECEIVE_DOMAIN_DATA,fromJS(result[0]))
        })
    }
    /**
     * 查询所有文章栏目
     */
    fetchArticleSections = ()=>{
       
        webapi.fetchArticleSections().then(result=>{
            this.dispatch(SectionTypes.FETCH_SECTIONS,fromJS(result))
        }).catch((error)=>{
            message.error(error.message,5);
            return Promise.reject(error);
        })
        
    }

    /**
     * 查询所有文章ArticleTypes
     */
    fetchArticles = ()=>{
         const pagination = this.state().get('pagination');
         const search = this.state().get('search');
        let posts = pagination.set('pageNum',pagination.get('pageNum') - 1).merge(search)
        return webapi.fetchArticles(posts.toJS()).then(result=>{
            const {dataList, ...pagination} = result;
            this.dispatch(ArticleTypes.FETCH_ARTICLES,fromJS(dataList))
            pagination.pageNum = pagination.pageNum + 1;
            this.dispatch(PaginationType.SET_PAGINATION_PARAM,fromJS(pagination))
            return result;
        }).catch(error=>{
            return Promise.reject(error);
        })
        
    }

    fetchArticle = (id) => {
        return webapi.fetchArticleDetail(id).then(result => {
            return result;
        })
    }
    /**
     * 新增文章列表
     * @param article 文章
     */
    addArticle = (article) =>{
        return webapi.submitArticle(article)
            .then((result)=>{
                //this.dispatch(ArticleTypes.ADD_ARTICLE,fromJS(result))
                message.success("添加文章成功",3)
                return result;
            })
            .catch(error=>{
                message.error(error.message || '新增文章失败');
                return Promise.reject(error);
            })
    }
    /**
     * 修改
     * @param section
     */
    updateArticle = (article) => {
       return webapi.submitArticle(article)
            .then(result=>{
                this.dispatch(ArticleTypes.UPDATE_ARTICLE,fromJS(article))
                message.success("修改文章成功",3)
                return result;
            })
            .catch(error=>{
                return Promise.reject(error);
            })
    }
    /**
     * 删除文章
     * @param id 文章编号
     */
    deleteArticle = (id) => {
        webapi.deleteArticle(id)
            .then(result=>{
                this.dispatch(ArticleTypes.DELETE_ARTICLE,id);
                this.fetchArticles();
            })
            .catch(error=>{
                return Promise.reject(error);
            })
    }

    changePage = (param) => {
        this.dispatch(PaginationType.CHANGE_PAGE_PARAM,fromJS(param))
    }

    changeSearchParam = (param) => {
        this.dispatch(SearchTypes.CHANGE_SEARCH_PARAM,fromJS(param));
    }
}